package com.mayisoftware.mayioa.common.impl.utils.file;

import com.mayisoftware.mayioa.common.impl.utils.DateUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 文件处理工具类
 *
 */
public class FileUtils
{

    /**
     *
     * @Title: writeFile
     * @Description: 写文件
     * @param @param filePath 文件路径
     * @param @param fileContent    文件内容
     * @return void    返回类型
     * @throws
     */
    public static void writeLog(String filePath,String content) {
        try {
            File f = new File(filePath);
            if (!f.getParentFile().exists()){
                f.mkdirs();
            }
            if (!f.exists()) {
                f.createNewFile();
            }
            StringBuilder sb = new StringBuilder();
            sb.append(content+"\r\n");
            OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f,true), "GBK");
            BufferedWriter writer = new BufferedWriter(write);
            writer.write(DateUtils.getTime()+":"+sb.toString());
            writer.flush();
            writer.close();
        } catch (Exception e) {
            System.out.println("写文件内容操作出错");
            e.printStackTrace();
        }
    }

    /**
     *
     * @Title: writeFile
     * @Description: 写文件
     * @param @param filePath 文件路径
     * @param @param fileContent    文件内容
     * @return void    返回类型
     * @throws
     */
    public static void writeFile(String filePath,String title, List<String> fileContent) {
        try {
            File f = new File(filePath);
            if (!f.exists()) {
                f.createNewFile();
            }else{
                f.delete();
            }

            StringBuilder sb = new StringBuilder();
            sb.append(title+"\r\n");
            for (String item:fileContent) {
                sb.append(item+"\r\n");
            }
            OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f), "GB2312");
            BufferedWriter writer = new BufferedWriter(write);
            writer.write(sb.toString());
            writer.flush();
            writer.close();
        } catch (Exception e) {
            System.out.println("写文件内容操作出错");
            e.printStackTrace();
        }
    }
    public  static List<File> getFileList(String strPath, List<String> fileExtends){
        List<File> fileList =new ArrayList<>();
        File fileDir = new File(strPath);
        if (null != fileDir && fileDir.isDirectory()) {
            File[] files = fileDir.listFiles();

            if (null != files) {
                for (int i = 0; i < files.length; i++) {
                    // 如果是文件夹 继续读取
                    if (files[i].isDirectory()) {
                        fileList.addAll(getExcelFileList(files[i].getAbsolutePath()));
                    } else {
                        String strFileName = files[i].getAbsolutePath();

                        boolean foundFile = false;
                        for (String fileExtend:fileExtends) {
                            if (strFileName.endsWith(fileExtend)){
                                foundFile = true;
                            }
                        }
                        if (null != strFileName && foundFile) {
                            fileList.add(files[i]);
                        }
                    }
                }

            } else {
                if (null != fileDir) {
                    String strFileName = fileDir.getAbsolutePath();
                    boolean foundFile = false;
                    for (String fileExtend:fileExtends) {
                        if (strFileName.endsWith(fileExtend)){
                            foundFile = true;
                        }
                    }
                    if (null != strFileName && foundFile) {
                        fileList.add(fileDir);
                    }
                }
            }
        }
        // 定义的全去文件列表的变量
        return fileList;
    }

    public static List<File> getCsvFileList(String strPath) {
        List<String> fileExtends = new ArrayList<>();
        fileExtends.add(".csv");
        return getFileList(strPath,fileExtends);
    }
    public static List<File> getExcelFileList(String strPath) {
        List<String> fileExtends = new ArrayList<>();
        fileExtends.add(".xls");
        fileExtends.add(".xlsx");
        return getFileList(strPath,fileExtends);
    }
    /**
     * 输出指定文件的byte数组
     * 
     * @param filePath 文件
     * @return
     */
    public static void writeBytes(String filePath, OutputStream os) throws IOException
    {
        FileInputStream fis = null;
        try
        {
            File file = new File(filePath);
            if (!file.exists())
            {
                throw new FileNotFoundException(filePath);
            }
            fis = new FileInputStream(file);
            byte[] b = new byte[1024];
            int length;
            while ((length = fis.read(b)) > 0)
            {
                os.write(b, 0, length);
            }
        }
        catch (IOException e)
        {
            throw e;
        }
        finally
        {
            if (os != null)
            {
                try
                {
                    os.close();
                }
                catch (IOException e1)
                {
                    e1.printStackTrace();
                }
            }
            if (fis != null)
            {
                try
                {
                    fis.close();
                }
                catch (IOException e1)
                {
                    e1.printStackTrace();
                }
            }
        }
    }

    /**
     * 删除文件
     * 
     * @param filePath 文件
     * @return
     */
    public static boolean deleteFile(String filePath)
    {
        boolean flag = false;
        File file = new File(filePath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists())
        {
            file.delete();
            flag = true;
        }
        return flag;
    }

    public static String getFileName(String filePath){
        if (filePath==null || filePath.length()==0){
            return null;
        }

        File file = new File(filePath);
        if (!file.exists()||!file.isFile()){
            return null;
        }

        String fileName= file.getName();
        return fileName.substring(0,fileName.lastIndexOf("."));
    }

    public static boolean copyFile(File source, File dest){

        try {
            org.apache.commons.io.FileUtils.copyFile(source, dest);
        }catch (Exception ex){
            return false;
        }
        return true;
    }


}
